<?php
/**
 * Introduction:  运费模板逻辑处理
 * @author: 杨陈鹏
 * @date: 2016/4/12 22:58
 * @email: yangchenpeng@cdlinglu.com
 */

namespace Common\Logic;

use Common\Model;

class TransportLogic
{

    static public $model, $extend;

    public function __construct()
    {

        self::$model = new Model\TransportModel();

        self::$extend = new Model\TransportExtendModel();

    }

    /**
     * Introduction: 获取运费模板
     * @author: 杨陈鹏
     * @date: 2016/4/15 14:12
     * @email: yangchenpeng@cdlinglu.com
     * @param int $
     */
    static public function getData($where, $filed1 = '*', $filed2 = '*')
    {


        if (!$where)
            return ['code' => 300, 'msg' => '参数错误'];
        //获取主表信息
        $master = self::$model
            ->where($where)
            ->field($filed1)
            ->select();

        if (!$master)
            return ['code' => 300, 'msg' => '暂无数据'];

        foreach ($master as $v)
            $ids .= $v['id'] . ',';

        $ids = rtrim($ids, ',');

        //获取附表数据
        $sleve = self::$extend
            ->where("transport_id IN($ids)")
            ->field($filed2)
            ->select();


        foreach ($master as $v) {

            foreach ($sleve as $val) {

                if ($val['transport_id'] == $v['id']) {

                    $v['transport'][] = $val;

                }

            }

            $data[] = $v;

        }

        return ['code' => 200, 'data' => $data];


    }

    /**
     * Introduction: 获取一条数据
     * @author: 杨陈鹏
     * @date: 2016/4/15 14:12
     * @email: yangchenpeng@cdlinglu.com
     * @param int $id 模板ID
     */
    static public function getOneData($id, $filed1 = '*', $filed2 = '*')
    {


        if (!$id)
            return ['code' => 300, 'msg' => '参数错误'];
        //获取主表信息
        $master = self::$model
            ->where(['id' => $id])
            ->field($filed1)
            ->find();

        if (!$master)
            return ['code' => 300, 'msg' => '暂无数据'];


        //获取附表数据
        $sleve = self::$extend
            ->where(['transport_id' => $master['id']])
            ->field($filed2)
            ->select();


        foreach ($sleve as $key => $val) {


            if ( $val['is_default'] == 1 ) {
                $master['default_transport'] = $val;

                unset($sleve[$key]);

                continue;


            }
            if ($val['transport_id'] == $master['id']) {

                $master['transport'][] = $val;

            }


        }

        return ['code' => 200, 'data' => $master];


    }

    /**
     * Introduction: 创建模板  third_auth
     * @author: 杨陈鹏
     * @date: 2016/4/05 16:04
     * @email: yangchenpeng@cdlinglu.com
     */
    static public function createTransport($data)
    {

        if (!$data['title'])
            return ['code' => 300, 'msg' => '参数错误'];

        $data['update_time'] = NOW_TIME;



        if ($id = self::$model->add($data))
            return ['code' => 200, 'data' => ['id' => $id]];

        return ['code' => 300, 'msg' => '添加失败'];

    }

    /**
     * Introduction: 创建模板  third_auth
     * @author: 杨陈鹏
     * @date: 2016/4/05 16:04
     * @email: yangchenpeng@cdlinglu.com
     */
    static public function createTransportExtend($data)
    {

        if (!$data['transport_id'])
            return ['code' => 300, 'msg' => '参数错误!'];

        //计算价格  转化为最小单位为分
        $data['sprice'] = $data['sprice'] * 100;
        $data['xprice'] = $data['xprice'] * 100;


        $data['province_ids'] = '0';
        $data['county_ids'] = '0';
        $data['city_ids'] = '0';
        $data['province_names'] = '全国';
        $data['county_names'] = '全国';
        $data['city_names'] = '全国';
        if ($data['areas']) {


            $data['province_ids'] = ',';
            $data['county_ids'] = ',';
            $data['city_ids'] = ',';
            $data['province_names'] = ',';
            $data['county_names'] = ',';
            $data['city_names'] = ',';
            foreach ($data['areas'] as $key => $val) {

                $values = explode('-', $val);

                switch ($values[1]) {

                    case 'province':

                        $data['province_ids'] .= $key . ',';
                        $data['province_names'] .= $values[0] . ',';

                        break;

                    case 'county':

                        $data['county_ids'] .= $key . ',';
                        $data['county_names'] .= $values[0] . ',';

                        break;

                    case 'city':

                        $data['city_ids'] .= $key . ',';
                        $data['city_names'] .= $values[0] . ',';

                        break;

                }


            }

        }

        if (self::$extend->add($data))
            return ['code' => 200, 'msg' => '添加成功'];

        return ['code' => 300, 'msg' => '添加失败!'];

    }
    /**
     * Introduction: 修改
     * @author: 杨陈鹏
     * @email: yangchenpeng@cdlinglu.com
     */
    static public function update($data)
    {

        if(!intval($data['id']))
            return ['code' => 300, 'msg' => '参数错误!'];


        self::$model->where(['id' => $data['id']])->save(['title' => $data['title'],'update_time' => NOW_TIME]);


        foreach( $data['data'] as $v ){

            $datas = [];
            if ( $v['areas'] ) {

                foreach( $v['areas'] as $key => $val ) {

                    $values = explode('-', $val);
                    switch ($values[1]) {

                        case 'province':

                            $datas['province_ids'] .= $key . ',';
                            $datas['province_names'] .= $values[0] . ',';

                            break;

                        case 'county':

                            $datas['county_ids'] .= $key . ',';
                            $datas['county_names'] .= $values[0] . ',';

                            break;

                        case 'city':

                            $datas['city_ids'] .= $key . ',';
                            $datas['city_names'] .= $values[0] . ',';

                            break;

                    }

                }

            }


            $datas['is_default']        = $v['is_default'];
            $datas['snum']              = $v['snum'];
            $datas['sprice']            = intval((floatval($v['sprice']) * 100));
            $datas['transport_title']   = $v['transport_title'];
            $datas['xnum']              = $v['xnum'];
            $datas['xprice']            = intval(floatval($v['xprice']) * 100);

            if(isset($v['id']) && intval($v['id'])) {

                $id = $v['id'];
                unset($v['id']);

                self::$extend->where(['id' => $id])->save($datas);

            } else {

                $datas['transport_id'] = $data['id'];

                self::$extend->add($datas);

            }

        }

        return ['code' => 200, 'msg' => '更新成功!'];


    }


}